<template>
  <div>
    <button @click="submit">发起Post请求</button><br />
    <p>获取的数据：{{ res }}</p>
    <br />
    <button @click="submit1">发起Get请求</button><br />
    <p>获取的数据： {{ res1 }}</p>
    <!-- <form>
      username:<input type="text" name="username" /> <br />
      password:<input type="password" name="password" /><br />
    </form> -->
  </div>
</template>

<script>
const axios = require("axios");
export default {
  name: "post",
  data: function () {
    return {
      res: "res",
      res1: "res1",
    };
  },
  methods: {
    submit: function () {
      var that = this;
      axios
        .post("/user/login", {
          username: "rsy",
          password: "1234",
        })
        .then(function (response) {
          that.res = response.data;
          console.log(response);
        })
        .catch(function (error) {
          console.log(error);
        });
    },
    submit1: function () {
      var that = this;
      // 向给定ID的用户发起请求
      axios
        .get("/user/hello")
        .then(function (response) {
          that.res1 = response.data;
          // 处理成功情况
          console.log(response.data);
        })
        .catch(function (error) {
          // 处理错误情况
          console.log(error);
        })
        .then(function () {
          // 总是会执行
        });
    },
  },
};
</script>